﻿#include "mainframe.h"
#include "ui_mainframe.h"
#include "systemmenu.h"

#include <QFile>
#include <QToolButton>
#include <QButtonGroup>
#include <QStackedWidget>
#include <QVBoxLayout>

MainFrame::MainFrame(QWidget *parent) :
    DragableShadowWidget(parent),
    ui(new Ui::MainFrame(this)),
    m_sysmenu(Q_NULLPTR)
{
    initUI();
    createConnections();
}

MainFrame::~MainFrame()
{
}

/**
 * @brief
 *   绘制原理为：
 *   1.外面是一个ShadowFrame用于实现边框，里面是一个RoundFrame，用于实现圆角.
 *   2.同时实现圆角和边框目前通过一个ShadowFrame无法实现，因为自绘的圆角边框会被其他widget覆盖.
 *   而且自绘的圆角边框还必须角度和圆角统一，那样设计很不好.
 *   3.外面ShadowFrame的必须背景透明，因为里面需要圆角，会让背景露出来.
 */
void MainFrame::initUI()
{
    resize(900, 650);
    setMinimumWidth(900);

    // 加载内容
    ui->setupUi(this);

    // 加载样式
    QFile qss(":/qss/mainframe");
    qss.open(QIODevice::ReadOnly);
    setStyleSheet(qss.readAll());
}

void MainFrame::createConnections()
{
    connect(ui, SIGNAL(showMinimized()), this, SLOT(showMinimized()));
    connect(ui, SIGNAL(showMaximized()), this, SLOT(showMaximized()));
    connect(ui, SIGNAL(showNormal()), this, SLOT(showNormal()));
    connect(ui, SIGNAL(close()), this, SLOT(close()));
    connect(ui->m_navBar_buttons, SIGNAL(buttonToggled(int,bool)),
            this, SLOT(onNavButtonToggled(int, bool)));
}



/**
 * @brief 按钮的作用是导航，所以这里叫NavButton.
 */
void MainFrame::onNavButtonToggled(int index, bool is_checked)
{
    if (is_checked)
    {
        switchPage(index);
    }
}

void MainFrame::switchPage(int page_index)
{
    Q_ASSERT(page_index < ui->m_page_container->count());

    ui->m_page_container->setCurrentIndex(page_index);
}
